﻿using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace LY.DataAccess.Repository
{

    /// <summary>
    /// 收货地址
    /// </summary>
    public class UserAddressRepository : BaseRepository
    {
        /// <summary>
        /// 地址列表
        /// </summary>
        /// <param name="MemberId">会员ID</param>
        /// <returns></returns>
        public List<U_UserAddress> GetAllUserAddress(int memberId)
        {
            var sql = "Select * from U_UserAddress where MemberId=@MemberId";
            return base.Query<U_UserAddress>(sql, new { MemberId = memberId });
        }

        //获取地址列表信息
        public PagedList<U_UserAddress> GetUserAddressList(int id, PageView view)
        {
            string cols = @" [Id]
                          ,[MemberId]
                          ,[Name]
                          ,[Tel]
                          ,[Address]
                          ,[Addtime]
                          ,[IsDefault]";
            string sqlWhere = " and MemberId=" + id;
            return base.PageGet<U_UserAddress>(view, cols, @" [U_UserAddress] ", sqlWhere, "[IsDefault] DESC", "");
        }

        /// <summary>
        /// 保存地址
        /// </summary>
        /// <param name="model">地址实体</param>
        /// <returns></returns>
        public int SaveUserAddress(U_UserAddress model)
        {
            if (model.Id > 0)
            {
                return (int)base.Update(model);
            }
            else
            {
                return (int)base.Insert(model);
            }

        }
        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="Id">主键ID</param>
        /// <returns></returns>
        public int DeleteUserAddress(int Id)
        {
            string sql = " DELETE FROM [U_UserAddress] WHERE ID=@ID";
            return base.Excute(sql, new { ID = Id });
        }
        /// <summary>
        /// 设置默认收货地址
        /// </summary>
        /// <param name="Id">主键ID</param>
        /// <returns></returns>
        public int SetDefaultAddress(int Id)
        {

            string sql = "Update U_UserAddress set IsDefault='1' WHERE ID=@ID";
            return base.Excute(sql, new { ID = Id });
        }
        /// <summary>
        /// 设置所有的为不默认
        /// </summary>
        /// <returns></returns>
        public int UpdateDefaultAddress(int? memberid)
        {

            string sql = "Update U_UserAddress set IsDefault='0' where MemberId=@MemberId";
            return base.Excute(sql, new { MemberId = memberid });
        }


        public U_UserAddress GetAddress(int Id)
        {
            string sql = "SELECT [Id],[MemberId],[Name],[Tel],[Address],[Addtime],[IsDefault] FROM [U_UserAddress] where Id=@Id";
            return base.Get<U_UserAddress>(sql, new { Id = Id });
        }



        //查询用户默认收货地址
        public U_UserAddress GetDefaultAddress(int memberId)
        {
            string sql = @"select [Id],[MemberId],[Name],[Tel],[Address],[Addtime],[IsDefault] from U_UserAddress 
                           where IsDefault=1 and MemberId=@MemberId";
            return base.Get<U_UserAddress>(sql, new { MemberId = memberId });
        }




    }
}
